package com.douban.business;

import java.util.HashMap;
import java.util.Map;

import org.apache.xerces.xni.Augmentations;
import org.apache.xerces.xni.QName;
import org.apache.xerces.xni.XMLAttributes;
import org.apache.xerces.xni.XMLString;

import com.core.sax.SaxHandler;
import com.douban.business.vo.ItemVO;
import com.douban.business.vo.RecordingStatusVO;
import com.douban.constants.DoubanConstant;

public class DoubanItemListHandler extends SaxHandler {

	private Map map = new HashMap();
	private RecordingStatusVO rstatus = new RecordingStatusVO();
	private String viewType = null;
	
	
	public DoubanItemListHandler(){
		super();
	}
	
	public void startElement(QName element, XMLAttributes attrs,
			Augmentations augs) {
		
		//for now the logic seems to be
		//when the class name is item
		ItemVO itemvo = getItemVO();
		Recorder lr = null;
		
		// iterate all attributes and element name
		if(this.viewType==null){
			String viewType = attrs.getValue(DoubanConstant.CLASS.getValue());
			if(viewType!=null&&viewType.equalsIgnoreCase(DoubanConstant.LIST_VIEW_TYPE.getValue())){
				this.viewType = DoubanConstant.LIST_VIEW_TYPE.getValue();
				rstatus.setStartListRecording(true);
			}else if(viewType!=null&&viewType.equalsIgnoreCase(DoubanConstant.GRID_VIEW_TYPE.getValue())){
				this.viewType = DoubanConstant.GRID_VIEW_TYPE.getValue();
				rstatus.setStartListRecording(true);
			}
			
//			this.viewType = DoubanConstant.LIST_VIEW_TYPE.getValue();
//			rstatus.setStartListRecording(true);
		}else{
			// if the attribute local name is class and the value is "list-view"
			if(this.viewType.equalsIgnoreCase(DoubanConstant.LIST_VIEW_TYPE.getValue())){
				//  use list view recorder
				//		start List recording
				lr = new ListViewRecorder(itemvo, rstatus, element, attrs, augs, Recorder.START_ELEMENT);
				if(lr.getItemVO()!=null&&lr.getItemVO().getId()!=null){
					rstatus.setRecordingId(lr.getItemVO().getId());
					putItemVO(rstatus.getRecordingId(), lr.getItemVO());
				}
				
			}else if(this.viewType.equalsIgnoreCase(DoubanConstant.GRID_VIEW_TYPE.getValue())){
				//	or "grid-view"
				//  use grid-view recorder
				
			}
			
		}
	}
    private ItemVO getItemVO() {
    	ItemVO ret = null;
    	if(rstatus.getRecordingId()!=null){
    		ret = (ItemVO)map.get(rstatus.getRecordingId());
    		//if the itemvo for the id does not exist yet, create one
    		if(ret==null){
    			ret=new ItemVO();
    			putItemVO(rstatus.getRecordingId(), ret);
    		}
    	} else{
    		return new ItemVO();
    	}
    	
    	return ret;
	}
    
    private void putItemVO(String id, ItemVO item){
    	if(map==null){
    		map = new HashMap();
    	}
    	map.put(id, item);
    }

	public void endElement(QName element, Augmentations augs) {
		
		ItemVO itemvo = getItemVO();
		Recorder lr = null;
		// if the attribute local name is class and the value is "list-view"
		if(viewType!=null&&this.viewType.equalsIgnoreCase(DoubanConstant.LIST_VIEW_TYPE.getValue())){
			//  use list view recorder
			//		start List recording
			lr = new ListViewRecorder(itemvo, rstatus, element, null, augs, Recorder.END_ELEMENT);
			if(lr.getItemVO()!=null&&lr.getItemVO().getId()!=null){
				rstatus.setRecordingId(lr.getItemVO().getId());
				putItemVO(rstatus.getRecordingId(), lr.getItemVO());
			}
			
		}else if(viewType!=null&&this.viewType.equalsIgnoreCase(DoubanConstant.GRID_VIEW_TYPE.getValue())){
			//	or "grid-view"
			//  use grid-view recorder
			
		}
    }
	
    /** Characters. */
    public void characters(XMLString text, Augmentations augs){

		ItemVO itemvo = getItemVO();
		Recorder lr = null;
		// if the attribute local name is class and the value is "list-view"
		if(viewType!=null&&this.viewType.equalsIgnoreCase(DoubanConstant.LIST_VIEW_TYPE.getValue())){
			//  use list view recorder
			//		start List recording
			lr = new ListViewRecorder(itemvo, rstatus, text, null, augs, Recorder.CHARACTERS_ELEMENT);
			if(lr.getItemVO()!=null&&lr.getItemVO().getId()!=null){
				rstatus.setRecordingId(lr.getItemVO().getId());
				putItemVO(rstatus.getRecordingId(), lr.getItemVO());
			}
			
		}else if(viewType!=null&&this.viewType.equalsIgnoreCase(DoubanConstant.GRID_VIEW_TYPE.getValue())){
			//	or "grid-view"
			//  use grid-view recorder
			
		}
    } // characters(XMLString,Augmentations)

	public Map getMap() {
		return map;
	}

	public void setList(Map map) {
		this.map = map;
	}
	
	
	

}
